﻿package com.view 
{
	import com.Model.vo.WomanVo;
	import com.Model.WomanProxy;
	import com.utils.DebugTrace;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * @author Mr.zheng
	 */
	public class WomanMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "womanMediator_name";
		public function WomanMediator(mediatorName:String = null, viewComponent:Object = null) 
		{
			super(mediatorName, viewComponent);
			
		}
		override public function onRegister():void 
		{
			super.onRegister();
			addEvent();
			setMcNum();
		}
	
		override public function listNotificationInterests():Array 
		{
			return [ApplicationMediator.INITWOMAN, PaintMenuMediator.COLORCHANGED, PaintMenuMediator.HAIRCHANGED, 
			ClothMenuMediator.HEADWEARCHANGED, ClothMenuMediator.CLOTHCHANGED, ClothMenuMediator.HOODCHANGED,
			ClothMenuMediator.BRACELETCHANGED,ClothMenuMediator.NECKLACECHANGED,WomanProxy.SLIDER];
		}
		override public function handleNotification(notification:INotification):void 
		{
			super.handleNotification(notification);
			var name:String = notification.getName();
			var womanVo:WomanVo = facade.retrieveProxy(WomanProxy.NAME).getData() as WomanVo;
			var womanMc:MovieClip = viewComponent.womanMc;
			switch(name) {
				case ApplicationMediator.INITWOMAN:
					var intiWomanVo:WomanVo = new WomanVo();
					showMc(womanMc,intiWomanVo);
					break;
				case PaintMenuMediator.HAIRCHANGED:
					womanVo.hairNum = notification.getBody() as int;
					showMc(womanMc,womanVo);
					break;
				case PaintMenuMediator.COLORCHANGED:
					var numObj:Object = notification.getBody() as Object;
					var typeNum:int = numObj.outNum;
					switch(typeNum) {
						case 1:
							womanVo.eyesNum = numObj.inNum;
							break;
						case 2:
							womanVo.eyesShadowNum=numObj.inNum;
							break;
						case 3:
							womanVo.blushNum=numObj.inNum;
							break;
						case 4:
							womanVo.eyelash=numObj.inNum;
							break;
						case 5:
							womanVo.mouthNum=numObj.inNum;
							break;
					}
					showMc(womanMc,womanVo);
					break;
				case ClothMenuMediator.HOODCHANGED:
					womanVo.hoodNum = notification.getBody() as int;
					showMc(womanMc,womanVo);
					break;
				case ClothMenuMediator.HEADWEARCHANGED:
					womanVo.headwearNum = notification.getBody() as int;
					showMc(womanMc,womanVo);
					break;
				case ClothMenuMediator.CLOTHCHANGED:
					womanVo.clothNum = notification.getBody() as int;
					showMc(womanMc,womanVo);
					break;
				case ClothMenuMediator.BRACELETCHANGED:
					womanVo.braceletNum = notification.getBody() as int;
					showMc(womanMc,womanVo);
					break;
				case ClothMenuMediator.NECKLACECHANGED:
					womanVo.necklaceNum = notification.getBody() as int;
					showMc(womanMc,womanVo);
					break;
				case WomanProxy.SLIDER:
					//DebugTrace.dtrace("code info WomanMediator.as:slider value--" + notification.getBody());
					var sliderNum:int = int(notification.getBody());
					var redNum:Number = 1-sliderNum/ 100;
					var greenNum:Number =sliderNum*0.01;
					var blueNum:Number	= sliderNum*0.02;
					//trace(redNum, greenNum, blueNum);
					var matrix:Array = new Array();
					if (sliderNum == 1) {
						matrix = matrix.concat([1, 0, 0, 0, 0]); // red
						matrix = matrix.concat([0, 1, 0, 0, 0]); // green
						matrix = matrix.concat([0, 0, 1, 0, 0]); // blue
						matrix = matrix.concat([0, 0,0, 1,0 ]); // alpha
					}else {
						matrix = matrix.concat([redNum, 0, 0, 0, 0]); // red
						matrix = matrix.concat([0, greenNum, 0, 0, 0]); // green
						matrix = matrix.concat([0, 0, blueNum, 0, 0]); // blue
						matrix = matrix.concat([0, 0,0, 1,0 ]); // alpha
					}
					var filter:ColorMatrixFilter = new ColorMatrixFilter(matrix);
					var filterArr:Array = new Array();
					filterArr.push(filter);
					MovieClip(viewComponent.womanMc.hairMc).filters = filterArr;
					break;
			}
		}
		private function showMc(womanMc:MovieClip,womanVo:WomanVo):void {
			gotoFrame(womanMc.eyesMc, womanVo.eyesNum);
			gotoFrame(womanMc.blushMc, womanVo.blushNum);
			gotoFrame(womanMc.eyelashMc, womanVo.eyelash);
			gotoFrame(womanMc.eyesShadowMc, womanVo.eyesShadowNum);
			gotoFrame(womanMc.hairMc, womanVo.hairNum);
			gotoFrame(womanMc.mouthMc, womanVo.mouthNum);
			
			gotoFrame(womanMc.hoodMc, womanVo.hoodNum);
			gotoFrame(womanMc.clothMc, womanVo.clothNum);
			gotoFrame(womanMc.braceletMc, womanVo.braceletNum);
			gotoFrame(womanMc.necklaceMc, womanVo.necklaceNum);
			gotoFrame(womanMc.headwearMc, womanVo.headwearNum);
		}
		private function setMcNum():void {
			var womanMc:MovieClip = viewComponent.womanMc as MovieClip;
			MovieClip(womanMc.eyesMc).num = 1;
			MovieClip(womanMc.eyesShadowMc).num = 2;
			MovieClip(womanMc.blushMc).num = 3;
			MovieClip(womanMc.eyelashMc).num = 4;
			MovieClip(womanMc.mouthMc).num = 5;
		}
		private function gotoFrame(mc:MovieClip,num:int):void {
			mc.gotoAndStop(num);
		}
		private function addEvent():void
		{
			var womanMc:MovieClip = viewComponent.womanMc as MovieClip;
			MovieClip(womanMc.eyesMc).addEventListener(MouseEvent.CLICK, onMcClick);
			MovieClip(womanMc.eyesShadowMc).addEventListener(MouseEvent.CLICK, onMcClick);
			MovieClip(womanMc.blushMc).addEventListener(MouseEvent.CLICK, onMcClick);
			MovieClip(womanMc.eyelashMc).addEventListener(MouseEvent.CLICK, onMcClick);
			MovieClip(womanMc.mouthMc).addEventListener(MouseEvent.CLICK, onMcClick);
		}
		
		private function onMcClick(e:MouseEvent):void 
		{
			var mcNum:int = MovieClip(e.currentTarget).num as int;
			var paintMenuMc:MovieClip = facade.retrieveMediator(PaintMenuMediator.NAME).getViewComponent().paintMenuMc as MovieClip;
			if (paintMenuMc.outNum == mcNum) {
				
			}else {
				return;
			}
			
			var obj:Object = { };
			obj.inNum = paintMenuMc.inNum;
			obj.outNum = paintMenuMc.outNum;
			
			sendNotification(MouseMediator.PLAY);
			sendNotification(PaintMenuMediator.COLORCHANGED,obj);
		}
	}

}